<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();
class Captcha extends ParentCaptcha {

	var $start_image = null;
	var $image_blocks = array();
	var $font = '';
	var $code = 0;
	var $white_c = null;
	var $black_c = null;
	var $out_image = null;
	var $font_height = 35;

	function Captcha($code){
		$this->code = $code;
		$this->gen_start_image();
		$this->get_fonts();
		$this->type_letters();
		$this->gen_12_block();
		$this->invert_blocks();
		$this->implode_image();
		$this->out_image = $this->wave($this->out_image);
		$this->out_img($this->out_image);
	}

	function gen_start_image(){
		$this->start_image = imagecreate($this->width,$this->height);
		$this->white_c     = imagecolorallocate($this->start_image,mt_rand(155,255),mt_rand(155,255),mt_rand(155,255));
		$this->black_c     = imagecolorallocate($this->start_image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
	}
	function type_letters(){
		$imagebox = imagettfbbox($this->font_height,0,$this->font,$this->code);
		$xpos = ceil(($this->width  - ($imagebox[0] + $imagebox[2]) )/2);
		$ypos = ceil(($this->height - ($imagebox[1] + $imagebox[7]) )/2);

		imagettftext($this->start_image,$this->font_height,0,$xpos,40,$this->black_c,$this->font,$this->code);
	}

	function gen_12_block(){
		for ($i = 0;$i < 12;$i++){
			$this->image_blocks[$i] = imagecreatetruecolor(50,20);
			$this->copy_to_small($i);
		}
	}
	function copy_to_small($i){
		$array = array(array(0,0),array(0,20),array(0,40),array(50,0),array(50,20),array(50,40),array(100,0),array(100,20),array(100,40),array(150,0),array(150,20),array(150,40));
		imagecopy($this->image_blocks[$i],$this->start_image,0,0,$array[$i][0],$array[$i][1],50,20);
	}
	function invert_blocks(){
		$p = mt_rand(0,1);
		switch ($p){
			case 0:
				$this->do_invert(array(0,2,4,6,8,10));
				break;
			case 1:
				$this->do_invert(array(1,3,5,7,9,11));
				break;
		}
	}
	function do_invert($array){
		imagedestroy($this->start_image);
		foreach ($array as $key){
			$this->image_blocks[$key] = $this->imageinvert($this->image_blocks[$key]);
		}
	}
	function imageinvert($picture, $x1=0, $y1=0, $x2='a', $y2='a') {
		if ($x2 == 'a') $x2=@imagesx($picture);
		if ($y2 == 'a') $y2=@imagesy($picture);

		for ($i=$x1; $i<$x2; $i++) {
			for ($j=$y1; $j<$y2; $j++) {
				$prgb = @imagecolorat($picture, $i,$j);
				$r1 = ($prgb >> 16) & 0xFF;
				$r2 = 255-$r1;
				$g1 = ($prgb >> 8) & 0xFF;
				$g2 = 255-$g1;
				$b1 = $prgb & 0xFF;
				$b2 = 255-$b1;
				$n_color = @imagecolorallocate($picture, $r2, $g2, $b2);
				@imagesetpixel($picture, $i, $j, $n_color);
			}
		}
		return $picture;
	}
	function implode_image(){
		$array = array(array(0,0),array(0,20),array(0,40),array(50,0),array(50,20),array(50,40),array(100,0),array(100,20),array(100,40),array(150,0),array(150,20),array(150,40));
		$this->out_image = imagecreatetruecolor(200,60);
		for ($i = 0;$i < 12;$i++){
			imagecopy($this->out_image,$this->image_blocks[$i],$array[$i][0],$array[$i][1],0,0,50,20);
		}
	}
}
